import { ref } from 'vue'

export const useFingerprint = () => {
    const deviceId = ref<string>('')

    const getFingerprint = async () => {
        if (process.server) return // 跳过服务端
        const fpPromise = (await import('@fingerprintjs/fingerprintjs')).load()
        const fp = await fpPromise
        const result = await fp.get()
        deviceId.value = result.visitorId
    }

    async function visitorLogin() {
        const { data:info } = await useFetchPost('/api/user/visitorLogin', {
            deviceId: deviceId.value
        })
        return info.value.data
    }

    return { deviceId, getFingerprint, visitorLogin }
}